package com.takanafu.haru.code{
	import com.takanafu.haru.core.GlobalManager;
	
	import mx.collections.ArrayCollection;
	
	public class CodeFunctionCall extends CodeBase{
		private var symbol:CodeSymbol;
		private var list:ArrayCollection;
		
		public function CodeFunctionCall(sym:CodeSymbol,l:ArrayCollection){
			this.symbol = sym;
			this.list = l;
		}
		override public function run():CodeBase{
			var c:CodeBase = GlobalManager.getSymbolValue(this.symbol);
			
			if(c == null){
				throw new Error("method");
			}
			if((c is CodeFunction) == false){
				throw new Error("method");
			}
			var fun:CodeFunction = c as CodeFunction;
			var list2:ArrayCollection = new ArrayCollection();
			var count:int = this.list.length;
			
			for(var i:int = 0;i < count;i++){
				var c1:CodeBase = this.list.getItemAt(i) as CodeBase;
				var c2:CodeBase = c1 != null ? c1.run() : null;
				
				if(c2 != null){
					list2.addItem(c2);
				}
			}
			fun.thread = this.thread;
			
			return fun.call(list2);
		}
	}
}